//
//  MYPickerViewController.m
//  PickerView
//
//  Created by cuiwenjuan on 15-1-4.
//  Copyright (c) 2015年 anytum. All rights reserved.
//

#import "MYPickerViewController.h"
#import "MYPickerView.h"

@interface MYPickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

{
    NSArray * dic;
    
    NSMutableArray * provinceArray;
    
    NSArray * cityArray;
    
    UIPickerView * picker;
    
    MYPickerView * pickerV;
   
    UILabel * label ;
}

@end

@implementation MYPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self initPickerView];
    
//    [self createPicker];
    
    [self createButton];
    
}

//button

-(void)createButton
{
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(40, 60, 200, 40)];
    
    button.layer.cornerRadius = 10.0;
    
    button.backgroundColor = [UIColor orangeColor];
    
    [button setTitle:@"显示pickerView" forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    
    
    UIButton * buttonS = [[UIButton alloc]initWithFrame:CGRectMake(40, 360, 200, 40)];
    
    buttonS.layer.cornerRadius = 10.0;
    
    buttonS.backgroundColor = [UIColor orangeColor];
    
     [buttonS setTitle:@"显示选中的省市" forState:UIControlStateNormal];
    
    [buttonS addTarget:self action:@selector(buttonSClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:buttonS];
    
    label = [[UILabel alloc]initWithFrame:CGRectMake(10, 410, self.view.frame.size.width-20, 40)];
    label.backgroundColor = [UIColor orangeColor];
    
    [self.view addSubview:label];
    
    
    
}

-(void)buttonSClick
{
//    NSLog(@" pro = %@ , city = %@",[pickerV getProvince],[pickerV getCity]);
    
    label.text = [NSString stringWithFormat:@"%@ and %@",[pickerV getProvince],[pickerV getCity]];
}

-(void)buttonClick
{
    pickerV = [MYPickerView sharePickerView];
    
    pickerV.frame = CGRectMake(5, 60,self.view.frame.size.width-20, 300);
    
    picker.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:pickerV];
    
    [pickerV createPickerView];
}

#pragma mark - pickerV

//pickerView
-(void)createPicker
{
    picker = [[UIPickerView alloc]initWithFrame:CGRectMake(10, 300, self.view.frame.size.width-20, 300)];
    picker.backgroundColor = [UIColor grayColor];
    picker.layer.cornerRadius = 10.0;
    picker.delegate = self;
    picker.dataSource = self;
    
    [self.view addSubview:picker];
}

//初始化数据
-(void)initPickerView
{
    NSBundle * bundle = [NSBundle mainBundle];
    
    NSString * plistPath = [bundle pathForResource:@"city.plist" ofType:nil];
    
    dic =[[NSArray alloc]initWithContentsOfFile:plistPath];
    
    cityArray = [[dic objectAtIndex:0] objectForKey:@"cities"];
    
    provinceArray = [[NSMutableArray alloc]init];
    
    [dic enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [provinceArray addObject:[obj objectForKey:@"state"]];
    }];
    
    
//    NSLog(@"ooodic = %@",dic);

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - pickerView 协议方法
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
    {
        return [provinceArray count];
    }else
    {
        return [cityArray count];
    }

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//   return [NSString stringWithFormat:@"%d,%d",(int)row,(int)component];
    
    if(component == 0)
    {
        return [provinceArray objectAtIndex:row];
    }
    else
    {
        return [cityArray objectAtIndex:row];
    }
}

//选中后的列
-(void)pickerView: (UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%d and %d",(int)row,(int)component);
    
    
    if(component == 0)
    {
        cityArray = [[dic objectAtIndex:row] objectForKey:@"cities"];
        
        //更新第二轮的数据
        [picker reloadComponent:1];
//        [picker reloadComponent:2];
        
        //设置第二列自动从0 开始
        [picker selectRow:0 inComponent:1 animated:YES];
    }
    else
    {
       
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
